package game.leon.engine.script;

import java.io.*;

/**
 * User: bianpeng
 * Date: 12-2-9
 */
public class FileSource implements Source {
    private InputStream input;
    private byte[] buf;
    private int bufsize;
    public FileSource(String file, int bufsize) {
        try {
            input = new BufferedInputStream(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        this.bufsize = bufsize;
        buf = new byte[bufsize >> 1];
    }
    public FileSource(InputStream input, int bufsize) {
        this.input = input;
        this.bufsize = bufsize;
        buf = new byte[bufsize >> 1];
    }
    @Override
    public int fillBuffer(Buffer buffer) {
        try {
            int n = input.read(buf);
            if (n <= 0) return -1;
            buffer.fill(buf, 0, n);
            return n;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }
    @Override
    public boolean eof() {
        return false;
    }

    @Override
    public int bufsize() {
        return this.bufsize;
    }
}
